package com.summer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.summer.common.OperateThreadLocal;
import com.summer.common.R;
import com.summer.entity.ShoppingCart;
import com.summer.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 加入购物车
     * @param shoppingCart 获取购物车数据
     * @return 返回购物数据
     */
    @PostMapping("/add")
    public R<ShoppingCart> addDishOrSetMeal(@RequestBody ShoppingCart shoppingCart){
        log.info("加入购物车请求，shoppingCart={}",shoppingCart.toString());
        //1. 判断购物车中是否存在此菜品或套餐 select * from shoppingCart where dishId/SetMealId = ? and userId = ?
        Long userId = OperateThreadLocal.getLocalThreadAttribute();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);
        //1.1 判断添加的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if (dishId != null){
            // 是菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
        }else {
            // 是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //1.2 调用service查询
        ShoppingCart shoppingCart1 = shoppingCartService.getOne(lqw);

        //2. 若存在则number+1，否则添加数据到购物车表
        if (shoppingCart1 != null){
            // 存在 更新shoppingCart1数量+1
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
            shoppingCartService.updateById(shoppingCart1);
        }else{
            // 不存在 封装shoppingCart 添加到数据库
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCart1 = shoppingCart; //调用完添加操作后自动填充属性也被赋值到shoppingCart中
        }
        return R.success(shoppingCart1);
    }

    /**
     * 根据用户id查询购物车数据
     * @return 返回数据列表
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> getShoppingCartList(){
        log.info("根据用户id查询购物车请求...");
        //1. 调用service查询
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, OperateThreadLocal.getLocalThreadAttribute());
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lqw);
        //2. 返回数据列表
        return R.success(shoppingCartList);
    }

    /**
     * 减少一个菜品或套餐
     * @return 返回菜品或套餐数据
     */
    @PostMapping("/sub")
    public R<ShoppingCart> subDishOrSetMeal(@RequestBody ShoppingCart shoppingCart) {
        log.info("减少一个菜品或套餐请求，请求菜品为shoppingCart={}",shoppingCart.toString());
        //1. 获取请求参数
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        //2. 根据请求菜品或套餐查询数据信息,再修改number+1：select * from shopping_cart where userId = ? and dishId/setmealId = ?
        //2.1 判断是菜品还是套餐
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, OperateThreadLocal.getLocalThreadAttribute());
        if (dishId != null){
            // 是菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else if (setmealId != null) {
            // 是套餐
            lqw.eq(ShoppingCart::getSetmealId, setmealId);
        }
        //2.2 调用service查询
        ShoppingCart shoppingCart1 = shoppingCartService.getOne(lqw);
        if (shoppingCart1 != null){
            //2.3 num+1并修改
            shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
            shoppingCartService.updateById(shoppingCart1);
        }

        //3. 异常处理，dishId 和setmealId可能都为null
        //3 返回购物车信息
        return R.success(shoppingCart1);
    }

    /**
     * 根据用户id清空购物车
     * @return 返回清空成功信息
     */
    @DeleteMapping("/clean")
    public R<String> deleteByUserId(){
        log.info("清空购物车...");
        // 调用service删除
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, OperateThreadLocal.getLocalThreadAttribute());
        shoppingCartService.remove(lqw);
        // 返回成功信息
        return R.success("删除成功!");
    }




}
